/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package slsBeans;

import EL.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Tian
 */
@Stateless
public class OrderBLO implements OrderBLORemote {
    
    @PersistenceContext(unitName = "Tunga_EJB3PU")
    private EntityManager em;

    public List<FoodOrders> getAll()
    {
        String hql = "From Order";
        Query query = this.em.createQuery(hql);
        List<FoodOrders> orders = query.getResultList();
        return orders ;
    }

    public boolean getByID(int id)
    {

        try {
            FoodOrders order = this.em.find(FoodOrders.class, id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        
    }

    

    public boolean edit(FoodOrders order)
    {
        try {

            this.em.merge(order);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean delete(String id)
    {
        
               return false;
    }

    public String add(Accounts acc, List<OrderDetails> orderDetails)
    {
        try
        {
            float total = 0;
            for (OrderDetails oDetail : orderDetails)
            {
                float money = oDetail.getQuantity() * oDetail.getFoods().getPrice();
                total += money;
            }
            String orderID = createOrderID();
            FoodOrders order = new FoodOrders(orderID, new Date(), Boolean.FALSE,total);
            List<FoodOrders> orders = new ArrayList<FoodOrders>();
            orders.add(order);
            acc.setFoodOrdersList(orders);
            order.setAccounts(acc);

            this.em.merge(acc);

            //get last order
            acc = this.em.find(Accounts.class, acc.getId());
            orders = acc.getFoodOrdersList();
            FoodOrders lastOrder = orders.get(orders.size() - 1);
            //insert order details temp
            for (OrderDetails oDetail : orderDetails)
            {
                List<OrderDetails> temp = new ArrayList<OrderDetails>();
                temp.add(oDetail);
                lastOrder.setOrderDetailsList(temp);

                oDetail.setFoodOrders(lastOrder);

                OrderDetailsPK orderDetailPK = new OrderDetailsPK(lastOrder.getId(), oDetail.getFoods().getId());
                oDetail.setOrderDetailsPK(orderDetailPK);
                this.em.merge(lastOrder);
            }
            return orderID;
        }
        catch (Exception e)
        {
            return null;
        }
            
    }
    public String createOrderID()
    {
        String result="";
        char[] chars = {'0','1','2','3','4','5','6','7','8','9','Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'};
        Random random = new Random();
        for(int i=1; i<=10; i++)
        {
            result += chars[random.nextInt(chars.length)];
        }
        return result;
    }
 
}
